package xcmg.syn.infra;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class JsonUtil{
	private JsonUtil() {
	}

	private static final Log log = LogFactory.getLog(JsonUtil.class);
  
  private static Gson gson = new Gson();

  public static String objectToString(Object paramObject){
	  if(paramObject == null){
		  return null;
	  }
	  String json = null;
	  try{
		  json = gson.toJson(paramObject);
	  }catch(Exception e){
		  log.error("ObjectToString error:"+e.getMessage());
	  }
	  return json;
  }
  
  public static <T> T stringToObject(String paramString, Class<T> paramClass){
	  if(StringUtils.isEmpty(paramString)){
		  return null;
	  }
	  T t = null;
	  try{
		   t = gson.fromJson(paramString, paramClass);
	  }catch(Exception e){
		  log.error("StringToObject error:"+e.getMessage());
	  }
	  return t;
  }
  
  public static <T> T stringToList(String paramString, Type paramType){
	  if(StringUtils.isEmpty(paramString)){
		  return null;
	  }
	  T t = null;
	  try{
		  t = gson.fromJson(paramString, paramType);
	  }catch(Exception e){
		  log.error("StringToList error:"+e.getMessage());
	  }
	  return t;
  }
  
  /**
   * @author I321533
   * @param json
   * @param clazz
   * @return
   */
  public static <T> List<T> jsonToList(String json, Class<T[]> clazz){
      Gson gson = new Gson();
      T[] array = gson.fromJson(json, clazz);
      return Arrays.asList(array);
  }

  /**
   * @param json
   * @param clazz
   * @return
   */
  public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz){
      Type type = new TypeToken<ArrayList<JsonObject>>(){}.getType();
      ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

      ArrayList<T> arrayList = new ArrayList<T>();
      for (JsonObject jsonObject : jsonObjects)
      {
          arrayList.add(new Gson().fromJson(jsonObject, clazz));
      }
      return arrayList;
  }

	/**
	 * json字符串转成map的
	 *
	 * @param gsonString
	 * @return
	 */
	public static <T> Map<String, T> gsonToMaps(String gsonString) {
		Map<String, T> map = null;
		if (gson != null) {
			map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
			}.getType());
		}
		return map;
	}

}